import apiurl from '@/config/urlConfig'
import { axios } from '@/utils/request'

class Service {
  /**
   * 题库管理
   * @returns {Promise<*>}
   */
  async page (params) {
    const { code, data } = await axios({
      url: apiurl.url.exam.itemBank.page,
      method: 'post',
      data: { ...params }
    }) || {}
    if (code === 200) {
      const { total, records } = data
      const { pageNumber, pageSize } = params
      return {
        total,
        records: records.map((m, inx) => ({ key: (pageNumber - 1) * pageSize + 1 + inx, ...m }))
      }
    }
    return { total: 0, records: [] }
  }

  async list () {
    const params = {
      _search: false,
      selectedType: 'question_source',
      type: 1
    }
    const { code, data } = await axios({
      url: apiurl.url.exam.itemBank.query,
      method: 'post',
      data: { ...params }
    }) || {}
    if (code === 200 && Array.isArray(data) && data.length > 0) {
      return data.map(({ id, examName }, inx) => ({ key: inx + 1, value: id, label: examName }))
    }
    return []
  }

  async add (params) {
    return await axios({
      url: apiurl.url.exam.itemBank.save,
      method: 'post',
      data: { ...params }
    }) || {}
  }

  async edit (params) {
    return await axios({
      url: apiurl.url.exam.itemBank.edit,
      method: 'post',
      data: { ...params }
    }) || {}
  }

  async del ({ id }) {
    return await axios({
      url: `${apiurl.url.exam.itemBank.del}/${id}`,
      method: 'post',
      data: {}
    }) || {}
  }
}

const itemBankService = new Service()
export default itemBankService
